  <?php $this->set('title_for_layout', $titles['Category']['name']); ?>
<div class="content_ads">
    <?php
        echo $this->element('top_content_ads');
    ?>
</div>
<?php //print_r($allbrands);
    //print_r($categories);
    if($categories) {
        echo '<h3 class="list-group-item">Classified Ads</h3>';
        echo '<div class="list-group-item"><div class="brands">';
        foreach($categories as $category) {
            $i = 0;
            echo '<div class="item">';
            //echo $category['Category']['name'];
            if($category['Category']['image']){
                echo '<img src="'.$this->webroot.'img/brand/'.$category['Category']['image'].'"/>';
            }else{
                echo '<img src="'.$this->webroot.'img/brand/no-brand.jpg'.'"/>';
            }
            foreach ($allbrands as $product) {
                if($category['Category']['id'] == $product['Product']['category_id']){
                    $i++;
                }
            }
            echo $this->Html->link($category['Category']['name'].'('.$i.')', array('controller' => 'categories', 'action' => 'cat', $category['Category']['id'], $category['Category']['parent_id']));
            echo '</div>';
        }
        echo '</div></div>';
    }
//print_r($products);
?>
<?php if($products!=null) {?>
<?php if($productPromotion) {?>
<div>
    <h3 class="list-group-item">Promotion Ads</h3>
      <ul id="flexiselDemo3">
                <?php foreach ($productPromotion as $product):
			echo '<li style="width: 200px;">';
			$image=$product['Product']['picture1'];
                        if(empty($image)){
                            $image = 'default_product.png';
                            $resizedFile = 'img/uploads/'.$image;
                        }else {
                           $resizedFile = 'img/uploads/images_resize/'.$image;
                        }
                        //echo $this->Html->link('<i class="promotion"></i>'.$this->Html->image('/img/uploads/images_resize/'.$image), array('controller'=>'posts', 'action' => 'view', $product['Product']['id']), array('escape' => false));
                        echo '<a href="'.$this->webroot.'posts/view/'.$product['Product']['id'].'"><i class="'.$product['Product']['type'].'"></i><img style="width: 100px; "src="'.$this->webroot.$resizedFile.'"/></a>';
                        if (strlen($product['Product']['title']) > 10) {
                            echo '<span class="pro-title">'.$this->Html->link(substr($product['Product']['title'], 0, 10).'...', array('controller' => 'posts', 'action' => 'view', $product['Product']['id'])).'</span>';
                        }else {
                            echo '<span class="pro-title">'.$this->Html->link($product['Product']['title'], array('controller' => 'posts', 'action' => 'view', $product['Product']['id'])).'</span>';
                        }
			echo '<span id="pro-price">$'.$product['Product']['price'].'<i class="'.$product['Product']['status'].'"></i></span>';
                       echo '<span id="auth">'.$product['Product']['name'].'</span><br>';
                        if($product['Location']['name']) {echo '<span id="address">'.$product['Location']['name'].'</span><br>';}
			echo '<span id="phone">'.$product['Product']['phone'].'</span><br>';
			echo '</li>';
		endforeach;?>
        </ul>
    <div class="clearout"></div>
</div>
<?php }?>
<div class="content_ads">
    <?php
        echo $this->element('middle_content_ads');
    ?>
</div>
<div class="list-group product-list">
    <div class="list-group-item" id="filter_box">
    <?php echo $this->Form->create('Posts'); ?>
                        <label>Filter by: </label>
                         <select id="first" class="form-control" name="data[Product][type]">
                                <option  value="0">-All Sale & Buy</option>
                                <option  value="sale">Sale</option>
                                <option  value="buy">Buy</option>
                        </select>
              
                       <?php echo $this->Form->input('location_id', array('div'=>false, 'class'=>'form-control','label' => false, 'name'=>'data[Product][location]', 'id'=>'second', 'width'=>'125', 'empty' => array(0 => '-All Locations'))); ?>
                     
                         <select id="third" class="form-control" name="data[Product][status]">
                                <option value="0">-All New & Used</option>
                                <option   value="new">New</option>
                                <option  value="used">Used</option>
                        </select>
              
                        <select id="fouth" class="form-control" name="data[Product][price]">
                                <option value="0">-All Price</option>
                                <option value="0-20">$0.01 - $20</option>
                                <option value="20-30">$20 - $30</option>
                                <option  value="30-50">$30 - $50</option>
                                <option  value="50-100">$50 - $100</option>
                                <option  value="100-200">$100 - $200</option>
                                <option  value="200-300">$200 - $300</option>
                                <option  value="300-500">$300 - $500</option>
                                <option  value="500-700">$500 - $700</option>
                                 <option  value="700-1000">$500 - $700</option>
                                <option  value="1000-1500">$1000 - $1500</option>
                                <option  value="1500-2000">$1500 - $2000</option>
                                <option  value="2000-3000">$2000 - $3000</option>
                                 <option  value="3000-5000">$3000 - $5000</option>
                                 <option  value="5000-10000">$5000 - $10000</option>
                                 <option  value="10000-1000000">$10000 - over</option>
                        </select>
              
               
        </form>
    </div>
<h3 class="list-group-item">Ads List</h3>
<div class="tab-content">
    <?php  
   foreach ($products as $product):
            echo '<div class="item">';
            $image=$product['Product']['picture1'];
            if(empty($image)){
                $image = 'default_product.png';
                $resizedFile = 'uploads/'.$image;
            }else {
               $resizedFile = 'uploads/images_resize/'.$image;
            }
             echo $this->Html->link($this->Html->image($resizedFile), array('controller'=>'posts', 'action' => 'view', $product['Product']['id']), array('escape' => false));

            if (strlen($product['Product']['title']) > 30) {
                echo '<span class="pro-title">'.$this->Html->link(substr($product['Product']['title'], 0, 30).'...', array('controller' => 'posts', 'action' => 'view', $product['Product']['id'])).'</span>';
            }else {
                echo '<span class="pro-title">'.$this->Html->link($product['Product']['title'], array('controller' => 'posts', 'action' => 'view', $product['Product']['id'])).'</span>';
            }
            echo '<span id="pro-price">$'.$product['Product']['price'].'<i class="'.$product['Product']['status'].'"></i></span>';
            echo '<span id="modified_date">'.$product['Product']['modified_date'].'</span>';
            echo '<span id="auth">'.$product['Product']['name'].'</span>';
            if($product['Location']['name']) {echo '<span id="address">'.$product['Location']['name'].'</span>';}
            echo '<span id="phone">'.$product['Product']['phone'].'</span><br>';
            if (strlen($product['Product']['description']) > 150) {
                echo '<span id="dec">'.substr(strip_tags($product['Product']['description']), 0, 150).'...'.'</span>';
            }else {
                echo '<span id="dec">'.$product['Product']['description'].'</span>';
            }
            
            echo '<div class="total-view">';
            ?> <a href="#" class="<?php echo $product['Product']['id']; ?>"></a><a href="#" id="<?php echo $product['Product']['id']; ?>" data="<?php echo $product['Product']['id'] ?>" class="favorite"></a><?php
            if($product['Product']['view']) {
                echo '<span class="total-product">View :'.$product['Product']['view'].'</span>';
            }else {
                echo '<span class="total-product">View : 0</span>';
            }
            $i = 0;
            for($a=1; $a<=4; $a++){
                $picture = "picture$a";
                if($product['Product'][$picture]){
                    $i++;
                }
            }
            echo '<span class="total-image">'.$i.'</span>';

            echo '<span class="total-comment">'.count($product['Comment']).'</span>';
            $total = 0;
//            foreach ($counttoday as $today) {
//                if($product['Product']['id'] == $today['Statistic']['product_id']) {
//                    $total = $today['Statistic']['count'];
//                }
//            }
//            echo '<apan class="total-today"> Today viewed:'.$total.'</span>';
            echo '</div>';
            echo '<div class="clearfix"></div></div>';
    endforeach;
     ?>
</div>
</div>

<div class="paging">
    <?php
            echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
            echo $this->Paginator->numbers(array('separator' => ''));
            echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
   ?>

</div> <?php } ?>

<div class="content_ads">
    <?php
        echo $this->element('bottom_content_ads');
       ?>
</div>

<script>
  var _ROOT = "<?php echo $this->Html->url('/', true); ?>";
  $(function() {
   
   $('#first').change(function() {
        var first=$('#first').val(); 
    var second=$('#second').val();
    var third=$('#third').val();
    var fouth=$('#fouth').val(); 
    //// replace the ID_OF_YOUR_SELECT_BOX with the id to your select box given by Cake
      // var val = $(this).val();  // val is the drug id
       window.location = _ROOT + 'categories/filter/<?php echo $params ?>/' + first +'/'+second +'/'+third+'/'+fouth;
    });
    $('#second').change(function() {
    var first=$('#first').val(); 
    var second=$('#second').val();
    var third=$('#third').val();
    var fouth=$('#fouth').val(); // replace the ID_OF_YOUR_SELECT_BOX with the id to your select box given by Cake
     
      window.location = _ROOT + 'categories/filter/<?php echo $params ?>/' + first +'/'+second +'/'+third+'/'+fouth;
    });
    $('#third').change(function() {  
         var first=$('#first').val(); 
    var second=$('#second').val();
    var third=$('#third').val();
    var fouth=$('#fouth').val(); // replace the ID_OF_YOUR_SELECT_BOX with the id to your select box given by Cake
     
      window.location = _ROOT + 'categories/filter/<?php echo $params ?>/' + first +'/'+second +'/'+third+'/'+fouth;
    });
    $('#fouth').change(function() { 
    var first=$('#first').val(); 
    var second=$('#second').val();
    var third=$('#third').val();
    var fouth=$('#fouth').val(); // replace the ID_OF_YOUR_SELECT_BOX with the id to your select box given by Cake
     
      window.location = _ROOT + 'categories/filter/<?php echo $params ?>/' + first +'/'+second +'/'+third+'/'+fouth;
    });
});
</script>
